#! /bin/bash
# After `mvn clean package -am -pl build`, this script can help you to release `easeagent-dep.jar` to github.

RELEASE_ID=41698268
LIST_RELEASE_URL="https://api.github.com/repos/megaease/easeagent/releases"
RELEASE_URL="https://api.github.com/repos/megaease/easeagent/releases/${RELEASE_ID}"
UPLOAD_URL="https://uploads.github.com/repos/megaease/easeagent/releases/${RELEASE_ID}/assets?name=easeagent.jar"
TOKEN=""
AUTH_HEAD="Authorization: token $TOKEN"
LIB_PATH="build/target/easeagent-dep.jar"
ASSET_URL=`curl -X GET -H "$AUTH_HEAD" "$RELEASE_URL" | jq --raw-output '.assets[0].url'`

echo "Delete last asset is $ASSET_URL"

curl -X DELETE -H "$AUTH_HEAD" "$ASSET_URL"

MD5=`md5 -q $LIB_PATH`

echo "Update easeagent hash: $MD5"

curl -X PATCH -H "$AUTH_HEAD" -H "Content-Type: application/json" -d "{\"body\":\"MD5(easeagent.jar) = \`$MD5\`\"}" "$RELEASE_URL" > /dev/null

echo "Upload easeagent.jar"

curl -X POST -H "$AUTH_HEAD" -H "Content-Type: application/java-archive" --progress-bar --data-binary @$LIB_PATH "$UPLOAD_URL"